package com.example.qingyiapp.ui.activity.fragment.fragment_FindUI.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.qingyiapp.R;
import com.example.qingyiapp.service.entify.FindChoicenessOneBean;
import com.example.qingyiapp.ui.activity.fragment.fragment_FindUI.childfragment.Choiceness_childfragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Creadted by Met·Wang on 2019/9/19 17:05
 * Describe: NullPointerException Write
 */
public class FindChoicenessRecycler2Adapter extends RecyclerView.Adapter<FindChoicenessRecycler2Adapter.VHolder> {

    ArrayList<FindChoicenessOneBean.DataBean> list;
    Context context;

    public FindChoicenessRecycler2Adapter(ArrayList<FindChoicenessOneBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public VHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_choiceness_item_item, null);
        return new VHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull VHolder vHolder, int i) {
        vHolder.textView.setText(list.get(i).getName()+"");


        List<FindChoicenessOneBean.DataBean.PostsBean> posts = list.get(i).getPosts();
        ArrayList<Fragment> fragments=new ArrayList<>();
        for (int j = 0; j < posts.size(); j++) {
            Bundle bundle=new Bundle();
            bundle.putSerializable("bean",posts.get(j));
            Choiceness_childfragment fragment=new Choiceness_childfragment();
            fragment.setArguments(bundle);
            fragments.add(fragment);
        }
        vHolder.viewPager.setId(i+1);
        ChoicenessfragmentAdapter choicenessfragmentAdapter = new ChoicenessfragmentAdapter(((FragmentActivity) context).getSupportFragmentManager(), fragments);
        vHolder.viewPager.setAdapter(choicenessfragmentAdapter);
        vHolder.viewPager.setPageMargin(20);

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class VHolder extends RecyclerView.ViewHolder{
        TextView textView;
        ViewPager viewPager;


        public VHolder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.item_item_text_title);
            viewPager=itemView.findViewById(R.id.item_item_vp_content);

        }
    }
}
